package priv.pattern.compositemode.service;

import priv.pattern.compositemode.service.Shape;

import java.awt.*;

/*
 * 作者：m鹏h
 * 时间；2024/8/5 17:57
 * 描述：提供基本功能的抽象形状
 **/
public abstract class BaseShape implements Shape {
 public int x;
 public int y;
 public Color color;
 private boolean selected = false;

 protected BaseShape(int x, int y, Color color) {
  this.x = x;
  this.y = y;
  this.color = color;
 }

 @Override
 public int getX() {
  return x;
 }

 @Override
 public int getY() {
  return y;
 }

 @Override
 public int getWidth() {
  return 0;
 }

 @Override
 public int getHeight() {
  return 0;
 }

 @Override
 public void move(int x, int y) {
  this.x += x;
  this.y += y;
 }

 @Override
 public boolean isInsideBounds(int x, int y) {
  return x > getX() && x < (getX() + getWidth()) &&
          y > getY() && y < (getY() + getHeight());
 }

 @Override
 public void select() {
  selected = true;
 }

 @Override
 public void unSelect() {
  selected = false;
 }

 @Override
 public boolean isSelected() {
  return selected;
 }

 protected void enableSelectionStyle(Graphics graphics) {
  graphics.setColor(Color.LIGHT_GRAY);

  Graphics2D g2 = (Graphics2D) graphics;
  float[] dash1 = {2.0f};
  g2.setStroke(new BasicStroke(1.0f,
          BasicStroke.CAP_BUTT,
          BasicStroke.JOIN_MITER,
          2.0f, dash1, 0.0f));
 }

 protected void disableSelectionStyle(Graphics graphics) {
  graphics.setColor(color);
  Graphics2D g2 = (Graphics2D) graphics;
  g2.setStroke(new BasicStroke());
 }


 @Override
 public void paint(Graphics graphics) {
  if (isSelected()) {
   enableSelectionStyle(graphics);
  }
  else {
   disableSelectionStyle(graphics);
  }

  // ...
 }
}
